<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------

namespace adminmatrix\swoole;

use adminmatrix\swoole\concerns\InteractsWithHttp;
use adminmatrix\swoole\concerns\InteractsWithLock;
use adminmatrix\swoole\concerns\InteractsWithPools;
use adminmatrix\swoole\concerns\InteractsWithQueue;
use adminmatrix\swoole\concerns\InteractsWithRpcClient;
use adminmatrix\swoole\concerns\InteractsWithRpcServer;
use adminmatrix\swoole\concerns\InteractsWithServer;
use adminmatrix\swoole\concerns\InteractsWithSwooleTable;
use adminmatrix\swoole\concerns\InteractsWithTracing;
use adminmatrix\swoole\concerns\WithApplication;
use adminmatrix\swoole\concerns\WithContainer;
use think\console\Output;
/**
 * Class Manager
 */
class Manager
{
    use InteractsWithServer,
        InteractsWithSwooleTable,
        InteractsWithHttp,
        InteractsWithPools,
        InteractsWithRpcClient,
        InteractsWithRpcServer,
        InteractsWithQueue,
        InteractsWithTracing,
        InteractsWithLock,
        WithContainer,
        WithApplication;

    /**
     * Initialize.
     */
    protected function initialize(): void
    {
        $this->prepareTables();
        $this->preparePools();
        $this->prepareHttp();
        $this->prepareRpcServer();
        $this->prepareQueue();
        $this->prepareRpcClient();
        $this->prepareTracing();
        $this->prepareLock();
    }

    protected function printListenTable()
    {
        $list = [];

        // 获取 http 配置
        $httpEnable = $this->getConfig('http.enable', true);
        if ($httpEnable) {
            $list[] = [
                'type' => 'http',
                'host' => $this->getConfig('http.host', '0.0.0.0'),
                'port' => $this->getConfig('http.port', 8080),
                'worker_num' => $this->getConfig('http.worker_num', swoole_cpu_num()),
                'server_name' => "http://" . $this->getConfig('http.host', '0.0.0.0') . ":" . $this->getConfig('http.port', 8080),

            ];
        }

        // 获取 websocket 配置
        $wsEnable = $this->getConfig('websocket.enable', false);
        if ($wsEnable) {
            $list[] = [
                'type' => 'websocket',
                'host' => $this->getConfig('websocket.host', '0.0.0.0'),
                'port' => $this->getConfig('websocket.port', 8080),
                'worker_num' => $this->getConfig('websocket.worker_num', swoole_cpu_num()),
                'server_name' => "http://" . $this->getConfig('websocket.host', '0.0.0.0') . ":" . $this->getConfig('websocket.port', 8080),

            ];
        }

        // 使用 Output 输出
        $output = new Output();
        $totalWidth = 90;
        $output->writeln("\033[36m================================== Swoole 服务启动信息 ====================================\033[0m");
        $fields = [
            "项目名"         => $this->getConfig('app.name', 'ThinkPHP'),
            "环境"           => $this->getConfig('app.env', 'production'),
            "ThinkPHP版本"   => defined('\think\App::VERSION') ? \think\App::VERSION : 'unknown',
            "PHP版本"        => PHP_VERSION,
            "Swoole版本"     => SWOOLE_VERSION,
            "主进程PID"      => getmypid(),
            "启动时间"       => date('Y-m-d H:i:s'),
        ];
        foreach ($fields as $label => $value) {
            $left = "\033[36m" . $label . " : \033[0m";
            $leftLen = mb_strwidth($label . " : ", 'UTF-8');
            $rightLen = mb_strwidth($value, 'UTF-8');
            $padLen = $totalWidth - $leftLen - $rightLen;
            if ($padLen < 0) $padLen = 0;
            $output->writeln($left . str_repeat(' ', $padLen) . $value);
            $output->writeln(str_repeat('=', $totalWidth));
        }
        $output->writeln(str_repeat('-', $totalWidth));
        $output->writeln(sprintf("| %-10s | %-12s | %-6s | %-10s | %-30s |", "type", "host", "port", "worker_num", "server_name"));
        $output->writeln(str_repeat('-', $totalWidth));
        foreach ($list as $row) {
            $output->writeln(sprintf(
                "| %-10s | %-12s | %-6s | %-10s | %-30s |",
                $row['type'],
                $row['host'],
                $row['port'],
                $row['worker_num'],
                $row['server_name']
            ));
        }
        $output->writeln(str_repeat('-', $totalWidth) . "\n");
    }

}
